<template>
  <div class="login-view">
    <div class="login-header">
      <h2>欢迎回来</h2>
      <p>登录您的待办事项管理系统账号</p>
    </div>
    
    <el-form 
      ref="loginForm" 
      :model="loginData" 
      :rules="rules" 
      label-position="top"
    >
      <el-form-item label="用户名" prop="username">
        <el-input v-model="loginData.username" placeholder="请输入用户名" />
      </el-form-item>
      
      <el-form-item label="密码" prop="password">
        <el-input 
          v-model="loginData.password" 
          type="password" 
          placeholder="请输入密码" 
          show-password
        />
      </el-form-item>
      
      <el-form-item>
        <el-button 
          type="primary" 
          :loading="isLoading" 
          @click="handleLogin" 
          class="submit-btn"
        >
          登录
        </el-button>
      </el-form-item>
    </el-form>
    
    <div class="auth-links">
      <router-link to="/auth/register">没有账号？立即注册</router-link>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useUserStore } from '../stores/user'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'

const router = useRouter()
const userStore = useUserStore()
const loginForm = ref(null)
const isLoading = ref(false)

const loginData = reactive({
  username: '',
  password: ''
})

const rules = {
  username: [
    { required: true, message: '请输入用户名或邮箱', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6个字符', trigger: 'blur' }
  ]
}

const handleLogin = async () => {
  if (!loginForm.value) return
  
  await loginForm.value.validate(async (valid) => {
    if (valid) {
      isLoading.value = true
      try {
        console.log('开始登录请求，数据:', loginData)
        const success = await userStore.login(loginData)
        console.log('登录请求完成，结果:', success)
        console.log('当前token状态:', userStore.token)
        console.log('当前登录状态:', userStore.isLoggedIn)
        
        if (success) {
          ElMessage.success('登录成功')
          
          // 使用setTimeout确保token已经被正确设置
          setTimeout(async () => {
            try {
              console.log('准备跳转，token:', userStore.token)
              console.log('准备跳转，登录状态:', userStore.isLoggedIn)
              await router.push('/')
              console.log('跳转完成')
            } catch (routerError) {
              console.error('路由跳转错误:', routerError)
              ElMessage.error('页面跳转失败')
            }
          }, 300) // 增加延迟时间
        } else {
          ElMessage.error('登录失败，请检查用户名和密码')
        }
      } catch (error) {
        ElMessage.error('登录时发生错误')
        console.error('登录错误详情:', error)
      } finally {
        isLoading.value = false
      }
    }
  })
}
</script>

<style scoped>
.login-view {
  width: 100%;
}

.login-header {
  text-align: center;
  margin-bottom: 2rem;
}

.login-header h2 {
  font-size: 1.8rem;
  color: #303133;
  margin-bottom: 0.5rem;
}

.login-header p {
  color: #606266;
  font-size: 1rem;
}

.submit-btn {
  width: 100%;
  margin-top: 1rem;
  height: 40px;
  font-size: 16px;
}

.auth-links {
  margin-top: 1.5rem;
  text-align: center;
}

.auth-links a {
  color: #409eff;
  text-decoration: none;
  font-size: 0.9rem;
}

.auth-links a:hover {
  text-decoration: underline;
}

:deep(.el-form-item__label) {
  font-weight: 500;
}

:deep(.el-input__wrapper) {
  box-shadow: 0 0 0 1px #dcdfe6 inset;
}

:deep(.el-input__wrapper:hover) {
  box-shadow: 0 0 0 1px #c0c4cc inset;
}

:deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 1px #409eff inset !important;
}
</style>